<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Shop extends MY_Controller {
    private $shop;

    public function __construct() {
        parent::__construct();

        //check url
        $segments = $this->uri->segments;
        if ($segments[1] != 's') { show_404(); }

        //check shop exists
        $this->load->model('repo/shop_model', 'shop_model');
        $this->shop = $this->shop_model->getShopBySlug($segments[2]);
        if (count($this->shop) == 0) { show_404(); }

        //check view mode
        $mode = $this->input->get('m', true);
        if ($mode != false && $mode == 'preview') {
            $theme = $this->input->get('t', true);
            if ($theme == false) { show_404(); }
            $this->shop->template = $theme;
        }
    }

    public function _remap($method, $params = array()) {
        //ccheck template exist
        $theme = $this->shop->template;
        $theme_path = APPPATH . 'views/shop-themes/';
        if (!file_exists($theme_path . $theme . '/' . $theme . '.php')) { show_404(); }

        //get right theme
        include_once ($theme_path . $theme . '/' . $theme . '.php');
        if(file_exists($theme_path . $theme . '/' . 'vardef.php')) {
            include_once ($theme_path . $theme . '/' . 'vardef.php');
        }
        if(file_exists($theme_path . $theme . '/' . $theme . '_model.php')) {
            include_once ($theme_path . $theme . '/' . $theme . '_model.php');
        }
        $tmpClass = new $theme(array('shop' => $this->shop));
        if (method_exists($tmpClass, $method)) {
            $tmpClass->$method($params);
        } else {
            show_404();
        }
    }
}
?>